<!DOCTYPE html>
<HTML>
<HEAD>
	<TITLE>Planet Creation</TITLE>
</HEAD>
<BODY>
	<h1 align="center">Planet Creation</h1>
	<?php
		//Include of required files
		require_once ("constants.php");
		require_once ("planets_class.php");
		require_once ("functions.php");

		//if the script is called with 'planet_creation' active, open conection with the
		// database and insert a new planet with the data in POST
		if(isset($_POST['planet_creation'])) {

			//copy planet name and size to working variables
			$planet_name = $_POST['planet_name'];
			$planet_size = $_POST['planet_size'];
				
			//insert a new planet into the database calling the 'InsertPlanet' function
			if(planetIsValid($planet_size)){
				
				if($insert_error = planets::CreatePlanet($planet_name, $planet_size, DB_USER)){
					
					echo "<p align=\"center\"><strong>Error creating planet: " .$insert_error."</strong></p>";
				} 
				else {
					
					echo "<p align=\"center\"><strong>Planet '" . $planet_name . "' of size " . $planet_size . " created.</strong></p>";
				}
			}
				
		}
	?>
	
<!--
	HTML form for planet creation, it submits planet_name, planet_size and the switch
	planet_creation to himself using the 'post' method
-->
	<br>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
		<fieldset style="width:30%">
			<legend>Planet Name</legend>
			<input type="text" name="planet_name" size="20">
		</fieldset>
		<fieldset style="width:30%">
			<legend>Planet Size</legend>
			<select name="planet_size" size="1">
			<?php
				//build the size dropbox from 1 to the maximum planet size set in the
				// MAX_PLANET_SIZE constant
				$list = "";
				for($i=1; $i<=MAX_PLANET_SIZE; $i++) {
					$list .= "<option>$i</option>\n";
				}
				echo "$list";
			?>
			</select>
		</fieldset>
		<p><input type="submit" value="Submit" name="enviar">
		<input type="hidden" name="planet_creation" value=1></p>
	</form>

	<br>
	<p align="center"><a href="index.php">Back to List of Planets</a></p>
</BODY>
</HTML>
